﻿using System.Diagnostics;
using System.Threading.Tasks;
using System;
using System.Threading;

namespace TaskResult
{
    internal class TaskCalculate
    {
        /// <summary>
        /// 基础task运用
        /// </summary>
        public  static void Start() 
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();  //启动Stopwatch
            Console.WriteLine("Stopwatch is running:{0}", stopwatch.IsRunning);//获取当前Stopwatch的状态
            var task1 = Task.Run(() => Calculate(2));
            var task2 = Task.Run(() => Calculate(1));
            Console.WriteLine($"线程执行结束，task1线程结果为{task1.Result * 1000},task2线程结果为{task2.Result * 1000}");
            Console.WriteLine("The second RunTime:{0}", stopwatch.ElapsedMilliseconds);//这里面使用毫秒来输出
            Console.WriteLine("===================================================");
        }

        /// <summary>
        /// 耗时计算业务
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        static int Calculate(int param)
        {
            Thread.Sleep(param * 1000);
            Console.WriteLine($"线程休息{param}秒结束");
            return param * 1000;
        }

    }
}
